package com.abhinav.hibernate;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.abhinav.hibernate.domain.Category;
import com.abhinav.hibernate.domain.Stock;
import com.abhinav.hibernate.util.HibernateUtil;

public class App {
	
	/*static{
	    try {
	        Class.forName ("oracle.jdbc.OracleDriver");
	    } catch (ClassNotFoundException e) {
	        e.printStackTrace();
	    }
	}*/

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Maven + Hibernate + MySQL");
		System.out.println("Hibernate many to many (Annotation)");

		Session session = HibernateUtil.getSessionFactory().openSession();

		Transaction tx = session.beginTransaction();
		
		Stock stock = new Stock();
        stock.setStockCode("7152");
        stock.setStockName("ABHISHEK");
        
        Category category1 = new Category("CONSUMER", "CONSUMER COMPANY");
        Category category2 = new Category("INVESTMENT", "INVESTMENT COMPANY");
        
        session.save(category1);
        session.save(category2);
		
        Set<Category> categories = new HashSet<Category>();
        categories.add(category1);
        categories.add(category2);
        
        stock.setCategories(categories);
        
        session.save(stock);
        
		tx.commit();
		tx = null;
		HibernateUtil.shutdown();
		
		System.out.println("******* DONE ********");
	}

}
